package com.bw.controller;

import com.bw.domain.TBed;
import com.bw.domain.TDoctor;
import com.bw.domain.TDpt;
import com.bw.service.TBedService;
import com.bw.service.TDoctorService;
import com.bw.service.TDptService;
import com.bw.utils.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.KeyBoundCursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author bobo棒
 * @date 2024年01月02日 11:16
 */


@RequestMapping("type")
@RestController
public class TypeController {
    @Autowired
    TDptService tDptService;
    @Autowired
    TBedService tBedService;
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    TDoctorService tDoctorService;
  //增量同步
    @RequestMapping("dptList2")
    public Result dptList2(Integer id){


        TDpt dpt = tDptService.getById(id);//没有缓存则查询数据库并放入缓存中
        redisTemplate.opsForList().rightPush("dptList",dpt);
        return Result.success("增量成功");
    }

    @RequestMapping("bedList2")
    public Result bedList2(Integer id){

        TBed bed = tBedService.getById(id);//没有缓存则查询数据库并放入缓存中
        redisTemplate.opsForList().rightPush("bedList",bed);
        return Result.success("增量成功");
    }



    //全量
    @RequestMapping("dptList")
    public Result dptList(){
        List<TDpt> list=null    ;
        //判断redis中是否存在缓存
        if(redisTemplate.hasKey("dptList")){
            list= redisTemplate.opsForList().range("dptList",0,-1);
        }else{
            list=tDptService.list(); //没有缓存则查询数据库并放入缓存中
            redisTemplate.opsForList().rightPushAll("dptList", list);
        }

        return Result.success(list);
    }
    @RequestMapping("bedList")
    public Result bedList(){
        List<TBed> list=null    ;
        //判断redis中是否存在缓存
        if(redisTemplate.hasKey("bedList")){
            list= redisTemplate.opsForList().range("bedList",0,-1);
        }else{
            list=tBedService.list(); //没有缓存则查询数据库并放入缓存中
            redisTemplate.opsForList().rightPushAll("bedList", list);
        }

        return Result.success(list);
    }
    @RequestMapping("doctorList")
    public Result doctorList(){
        List<TDoctor> list=null    ;
        //判断redis中是否存在缓存
        if(redisTemplate.hasKey("doctorList")){
            list= redisTemplate.opsForList().range("doctorList",0,-1);
        }else{
            list=tDoctorService.list(); //没有缓存则查询数据库并放入缓存中
            redisTemplate.opsForList().rightPushAll("doctorList", list);
        }

        return Result.success(list);
    }
}
